package net.jarang.support.clock;

import java.util.Calendar;
import java.util.Date;

public abstract class Clock {
	public static Clock INSTALLED = new SystemClock();
	public static final long SECONDS = 1000L;
	public static final long MINUTES = 60 * SECONDS;
	public static final long HOURS = 60 * MINUTES;
	public static final long DAYS = 24 * HOURS;
	
	public static void install(Clock clock) {
		INSTALLED = clock;
	}

	/**
	 * @return in milliseconds.
	 */
	public abstract long now();
	
	public Date nowAsDate() {
		return new Date(now());
	}
	
	public Date asDate(long date) {
		return new Date(date);
	}
	
	public Calendar nowCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(now());
		return calendar;
	}

	static class SystemClock extends Clock {
		public long now() {
			return System.currentTimeMillis();
		}
	}
}
